{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 基础运算符、表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "参考资料:  \n",
    "[python基础 基础运算符与表达式](https://blog.csdn.net/weixin_45623093/article/details/104924638)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 基础运算符\n",
    "***\n",
    "<font size=3>\n",
    "  \n",
    "  ### 加减乘除\n",
    "  ***  \n",
    "编程的加减乘除与普通数学有所不同  \n",
    "有基础的盆友要注意python的运算与其他语言有所区别！！！  \n",
    "  ***  \n",
    "### **加法:使用 '+'**    \n",
    "' + ' 运算符不仅仅用于数学的加法还用于字符串的拼接等\n",
    "  ***  \n",
    "  ### **减法 使用 '-'**  \n",
    "' - '可以用于减法  \n",
    "  ***  \n",
    "### **乘法  使用'*'**  \n",
    "' * ' 使用方法  \n",
    "①用于数字的乘法  \n",
    "②用于字符串的多次输出  \n",
    "  ***  \n",
    "###   **除法 使用 ' / '**  \n",
    "普通数学除法    \n",
    "#有计算机编程基础的小哥哥小姐姐注意！！！**有余数**  \n",
    "\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "hello paddle\n",
      "1\n",
      "6\n",
      "PaddlePaddle\n",
      "3.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "# 加法\r\n",
    "print(1+2)\r\n",
    "print('hello '+'paddle')\r\n",
    "\r\n",
    "# 减法\r\n",
    "print(2-1)\r\n",
    "# 乘法\r\n",
    "print(2*3)\r\n",
    "print('Paddle'*2)\r\n",
    "# 除法\r\n",
    "print(10/3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 其他基础运算符\n",
    "***\n",
    "<font size=3>\n",
    "  \n",
    "  ### 整除 、乘幂 、取模\n",
    "  ***  \n",
    "整除：与其他的编程语言略微不同\n",
    "  乘幂：与数学基本相同\n",
    "  取模：就是传说中的取余数\n",
    "  ***  \n",
    "### **整除:使用 '//'**    \n",
    "' // ' 除法得到的结果取整数位\n",
    "  ***  \n",
    "  ### **乘幂 使用 '\\*\\*'**  \n",
    "' \\*\\* '和数学一样 \n",
    "  ***  \n",
    "### **取模  使用'%'**  \n",
    "' % '：就是取余数   \n",
    "* 有计算机编程基础的小哥哥小姐姐注意！！！**有余数**  \n",
    "\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "整除: 2\n",
      "乘幂： 25\n",
      "取余： 1\n"
     ]
    }
   ],
   "source": [
    "a = 5\r\n",
    "b = 2\r\n",
    "print('整除:', a // b)\r\n",
    "print('乘幂：', a ** b)\r\n",
    "print('取余：', a % b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 特殊运算符\n",
    "***\n",
    "<font size=3>\n",
    " \n",
    " \n",
    "  特殊算法是比较贴合计算机低层，在python中多用于脚本书写和一些算法    \n",
    "主要是一些二进制的运算！！！  \n",
    "  ***  \n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "\n",
    "### **向左移 使用' << '**  \n",
    "该符号是把数字转换为二进制然后向左移  \n",
    "  \n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2  <<  2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![ 2<<2解析 ](https://img-blog.csdnimg.cn/2020031717330976.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)\n",
    " >我们先把2转换为2进制“10”一共6位前面拿0补齐  \n",
    " 我们把10往前移2位后面0补齐得到“001000”  \n",
    " 得到结果8  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **向右移  使用' >> '**  \n",
    "与向右移差不多异曲同工  \n",
    "  该符号是把数字转换为二进制然后向右移\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "11 >> 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![右移解析](https://img-blog.csdnimg.cn/20200317173810659.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)  \n",
    ">11的二进制右移其他的0补齐，得到的结果基本上就是了  \n",
    "至于少掉的1应该理解为缺失了\n",
    "\n",
    "自己画的图有问题的及时提哟！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **按位与 使用 '&'**  \n",
    "都为1取1，有一个是0就是0\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 & 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![在这里插入图片描述](https://img-blog.csdnimg.cn/20200317181320980.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)\n",
    "> 都是1的为1有一个为0的就是0,补齐后就是结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **按位或  使用'|'**  \n",
    "有一个是1就是1，全部为0才是0\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 | 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![在这里插入图片描述](https://img-blog.csdnimg.cn/20200317181447178.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)\n",
    ">有一个是1就是1，全部为0才是0,补齐即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **按位异或 使用' ^ '**  \n",
    "相同的写0，不同的写1\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 ^ 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![在这里插入图片描述](https://img-blog.csdnimg.cn/20200317181617596.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **按位取反 使用' ~ '**  \n",
    "按位取反的结果等于-（n+1）（正负号都符合）\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "~5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![在这里插入图片描述](https://img-blog.csdnimg.cn/20200317183226989.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "以上内容如有不同看法或有错误请及时指出，三岁一定认真对待及时修改！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 比较符\n",
    "***  \n",
    "<font size=3>\n",
    "  \n",
    "  ###  **小于 使用 ' < '**  \n",
    "判断结果是否正确返回浮点型  \n",
    "  ###  **大于 使用'  > '**  \n",
    "判断结果是否正确返回浮点型  \n",
    "  ###  **大于等于 使用' >= '  小于等于 使用' <='**  \n",
    "判断结果是否正确返回浮点型  \n",
    "  ###  **赋值  使用' = '  等于使用 ' == '**  \n",
    "判断结果是否正确返回浮点型  \n",
    "  ###  **不等于 使用' != '**  \n",
    "判断结果是否正确返回浮点型，与等于相反  \n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 1\r\n",
    "b = 2\r\n",
    "\r\n",
    "print(a < b)  # 小于\r\n",
    "print(a > b)  # 大于\r\n",
    "print(a >= b)  # 大于等于\r\n",
    "print(a <= b)  # 小于等于\r\n",
    "print(a == b)  # 等于\r\n",
    "print(a != b)  # 不等于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  ###  **且 使用' and '**  \n",
    "两者或多者全部符合为真反之为假  \n",
    "当有一个为假时python将停止判断直接输出  \n",
    "该方式为“短路计算”  \n",
    "  ###  **或 使用 ' or '**  \n",
    "两者或多者有一者符合即为真，全部为假才是假 \n",
    "  ###  **非 使用' not '**  \n",
    "表示非,不是，相反的 \n",
    "  ###  **\"属于\" 使用 ' in '**  \n",
    "在字符串、字典、元组、数值等类型中可以用'in'和'not in'  \n",
    "判定值是否在该类型中 \n",
    " 相对应属于 在……中  \n",
    "  ###  **\"不属于\" 使用 'not in '**  \n",
    "在字符串、字典、元组、数值等类型中可以用'in'和'not in'  \n",
    "判定值是否在该类型中 \n",
    " 相对应不属于 不在……中\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "x = False\r\n",
    "y = True\r\n",
    "z = 'abcdef'\r\n",
    "w = 'a'\r\n",
    "print(x and y)   # 且\r\n",
    "print(x or y)  # 或\r\n",
    "print(not x)  # 非\r\n",
    "print(w not in z)  # 不属于\r\n",
    "print(w in z)  # 属于"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 数值缩写方式\n",
    "***  \n",
    "<font size=3>\n",
    "  \n",
    "  变量\t=\t变量\t运算\t表达式\t\t演变成\t\t变量\t运算\t=\t表达式\t  (两者相同)\n",
    "  *** \n",
    "  ###  **自加 使用' += '**  \n",
    "\n",
    "  ###  **自减 使用' -= '**  \n",
    "  ###  **自乘 使用' \\*= '** \n",
    "  ### **自除 使用' /= '**  \n",
    "  ### **自幂 使用' \\*\\*= '**  \n",
    "  ### **自模 使用' %= '**\n",
    "  ### **自整除 使用 ' //= '**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = a + 1 == a += 1\r\n",
    "a = a - 1 == a -= 1\r\n",
    "a = a * 1 == a *= 1\r\n",
    "a = a / 1 == a /= 1\r\n",
    "a = a ** 1 == a **= 1\r\n",
    "a = a % 1 == a %= 1\r\n",
    "a = a // 1 == a //= 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 各个运算的优先级\n",
    "***  \n",
    "与我们小学数学的先乘除后加减相类似\n",
    "***  \n",
    "![在这里插入图片描述](https://img-blog.csdnimg.cn/20210224155421480.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyMzA5Mw==,size_16,color_FFFFFF,t_70)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 作者简介\n",
    "> 作者：三岁  \n",
    "经历：自学python，现在混迹于paddle社区，希望和大家一起从基础走起，一起学习Paddle  \n",
    "csdn地址：https://blog.csdn.net/weixin_45623093/article/list/3  \n",
    "我在AI Studio上获得黄金等级，点亮7个徽章，来互关呀~ https://aistudio.baidu.com/aistudio/personalcenter/thirdview/284366  \n",
    "\n",
    ">传说中的飞桨社区最差代码人，让我们一起努力！  \n",
    "记住：三岁出品必是精品 （~~不要脸系列~~）\n",
    "\n",
    "注：以上内容如有问题请及时留言，会立即处理！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.0.0b0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
